package com.isman.placeslist.adapter;

import java.util.ArrayList;

import net.londatiga.android.QuickAction.OnActionItemClickListener;
import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;

import com.isman.placeslist.PageStatus;
import com.isman.placeslist.R;
import com.isman.placeslist.apiobject.Status;
import com.isman.placeslist.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class StatusAdapter extends BaseAdapter{
	public StatusAdapter(Activity mActivity, StatusAdapterEvent mAdapterEvent, ArrayList<Status> mStatusList) {
		this.mActivity = mActivity;
		this.mAdapterEvent = mAdapterEvent;
		this.mStatusList = mStatusList;
	}
	@Override
	public int getCount() {
		return mStatusList.size();
	}
	@Override
	public Object getItem(int index) {
		return mStatusList.get(index);
	}
	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	@Override
	public View getView(int index, View arg1, ViewGroup arg2) {
		Status mStatus = mStatusList.get(index);
		View mView = null;
		switch (mStatus.getStatusType()) {
		case 0:
		case 1:
			mView = LayoutInflater.from(mActivity).inflate(R.layout.inc_item_status_normal, null);
			findResource(mView, mStatus, index);
			break;
		case 2:
			mView = LayoutInflater.from(mActivity).inflate(R.layout.inc_item_status_checkin, null);
			findResource(mView, mStatus, index);
			break;
		}
		if(index%2==0){
			mView.setBackgroundColor(0xFFEFEFEF);
		}else{
			mView.setBackgroundColor(0xFFFFFFFF);
		}
		return mView;
	}
	private void findResource(View mView,final Status mStatus, final int index){
		TextView vlblMessage = (TextView)mView.findViewById(R.id.lbl_item_status_message);
		TextView vlblCreate = (TextView)mView.findViewById(R.id.lbl_item_status_createtime);
		TextView vlblName = (TextView)mView.findViewById(R.id.lbl_item_status_profilename);
		TextView vlblMore = (TextView)mView.findViewById(R.id.lbl_item_status_more);
		Button vBtnAdd = (Button)mView.findViewById(R.id.btn_item_status_add);
		final ImageView vimgProfile = (ImageView)mView.findViewById(R.id.img_item_status_profileimage);
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				final Bitmap bmp = Util.getBitmap(mStatus.getFrom().getPicture());
				mActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						vimgProfile.setImageBitmap(bmp);
					}
				});
			}
		}).run();
		
		
		
		if(mStatus.getStatusType() == 1){
			final ImageView vImgImage = (ImageView)mView.findViewById(R.id.img_item_status_image_priview);
			new Thread(new Runnable() {
				@Override
				public void run() {
					final Bitmap bmp = Util.getBitmap(mStatus.getImage().getSize120());
					mActivity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							vImgImage.setImageBitmap(bmp);
							vImgImage.setVisibility(View.VISIBLE);
						}
					});
				}
			}).run();
		}
		if(mStatus.getStatusType() == 2){
			
		}
		if(mStatus.getCurrentLocation() != null){
			TextView vlblCurrent = (TextView)mView.findViewById(R.id.lbl_item_status_current);
			vlblCurrent.setVisibility(View.VISIBLE);
			vlblCurrent.setText(mStatus.getCurrentLocation().getName());
		}
		
		if(mStatus.getChildCount() != 0 || mStatus.getLikeCount() != 0){
			vlblMore.setVisibility(View.VISIBLE);
			vlblMore.setText("");
			if(mStatus.getChildCount() == 1){
				vlblMore.setText(mStatus.getChildCount()+" Comment");
			}
			if(mStatus.getChildCount() > 1){
				vlblMore.setText(mStatus.getChildCount()+" Comments");
			}
			
			if(mStatus.getLikeCount() == 1){
				vlblMore.setText(vlblMore.getText()+" "+mStatus.getLikeCount()+" Like");
			}
			if(mStatus.getLikeCount() > 1){
				vlblMore.setText(vlblMore.getText()+" "+mStatus.getLikeCount()+" Likes");
			}
		}else{
			vlblMore.setVisibility(View.GONE);
		}
		
		
		if(!mStatus.getMessage().equals("")){
			vlblMessage.setText(mStatus.getMessage());
			vlblMessage.setVisibility(View.VISIBLE);
		}
		vlblCreate.setText(mStatus.getCreateTime());
		vlblName.setText(mStatus.getFrom().getName());
		
		vBtnAdd.setVisibility(View.GONE);
		vBtnAdd.setOnClickListener(new AddClick(mStatus,index));
		mView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mActivity, PageStatus.class);
				intent.putExtra(PageStatus.STATUS_INDEX, index);
				intent.putExtra(PageStatus.STATUS_INFO, mStatus);
				mActivity.startActivityForResult(intent, PageStatus.STATUS_DETAIL);
			}
		});
	}
	
    public class AddClick implements OnClickListener,OnActionItemClickListener{
    	private QuickAction quickAction;
    	private Status mStatus;
    	private int mIndex;
    	public AddClick(Status mStatus, int mIndex) {
    		this.mStatus = mStatus;
    		this.mIndex = mIndex;
    	}
		
		@Override
		public void onClick(View v) {
			if(mStatus.isCanLike()){
				if(mStatus.isCanDelete()){
					mQuickActionCanDelateAndCanLike();
					quickAction.show(v);
				}else{
					mQuickActionNotDelateAndCanLike();
					quickAction.show(v);
				}	
			}else{
				if(mStatus.isCanDelete()){
					mQuickActionCanDelateAndNotLike();
					quickAction.show(v);
				}else{
					mQuickActionNotDelateAndNotLike();
					quickAction.show(v);
				}	
			}
		}
	    private void mQuickActionCanDelateAndCanLike(){
			ActionItem like 	= new ActionItem(1, "Like", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			ActionItem comment 	= new ActionItem(0, "Comment", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			quickAction = new QuickAction(mActivity);
			quickAction.setOnActionItemClickListener(this);
			
			//add action items into QuickAction
	        quickAction.addActionItem(like);
	        quickAction.addActionItem(comment);
	    }
	    private void mQuickActionCanDelateAndNotLike(){
			ActionItem like 	= new ActionItem(3, "Unlike", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			ActionItem comment 	= new ActionItem(0, "Comment", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			quickAction = new QuickAction(mActivity);
			quickAction.setOnActionItemClickListener(this);
			
			//add action items into QuickAction
	        quickAction.addActionItem(like);
	        quickAction.addActionItem(comment);
	    }
	    private void mQuickActionNotDelateAndCanLike(){
			ActionItem like 	= new ActionItem(1, "Like", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			ActionItem comment 	= new ActionItem(0, "Comment", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			quickAction = new QuickAction(mActivity);
			quickAction.setOnActionItemClickListener(this);
			
			//add action items into QuickAction
	        quickAction.addActionItem(like);
	        quickAction.addActionItem(comment);
	    }
	    private void mQuickActionNotDelateAndNotLike(){
			ActionItem like 	= new ActionItem(3, "Unlike", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			ActionItem comment 	= new ActionItem(0, "Comment", mActivity.getResources().getDrawable(R.drawable.ic_menu_mylocation));
			quickAction = new QuickAction(mActivity);
			quickAction.setOnActionItemClickListener(this);
			
			//add action items into QuickAction
	        quickAction.addActionItem(like);
	        quickAction.addActionItem(comment);
	    }
	  
		@Override
		public void onItemClick(net.londatiga.android.QuickAction source,
				int pos, int actionId) {
			Intent intent = null;
			switch (actionId) {
			case 0:
				//Comment and more
				intent = new Intent(mActivity, PageStatus.class);
				intent.putExtra(PageStatus.STATUS_INDEX, mIndex);
				intent.putExtra(PageStatus.STATUS_INFO, mStatus);
				mActivity.startActivityForResult(intent, PageStatus.STATUS_DETAIL);
				break;
			case 1:
				//Like
				mAdapterEvent.mStatusLike(mIndex, mStatus);
				break;
			case 2:
				//Delete
				mDelete();
				break;
			case 3:
				//Unlike
				mAdapterEvent.mStatusUnlike(mIndex, mStatus);
				break;
			}
		}
		


		private void mDelete(){
			Toast.makeText(mActivity, "Delete", Toast.LENGTH_LONG).show();
		}
    }
    private ArrayList<Status> mStatusList;
    private StatusAdapterEvent mAdapterEvent;
    private Activity mActivity;
}
